package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

// Created on:	2-Jun-08
// Author:		Richard Shih

public class HardwareTestResult
{

	private int					id;		//TODO
	private String				type;
	private String				ipAddress;
	private String				results;
	private GregorianCalendar	startTime;
	private GregorianCalendar	endTime;
	private String				startTimeString;
	private String				endTimeString;

	public HardwareTestResult()
	{
		this.ipAddress = "";
		this.results = "";
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getStartTimeString() {
		return startTimeString;
	}

	public void setStartTimeString(String startTimeString) {
		this.startTimeString = startTimeString;
	}

	public String getEndTimeString() {
		return endTimeString;
	}

	public void setEndTimeString(String endTimeString) {
		this.endTimeString = endTimeString;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	public String getResults()
	{
		return results;
	}

	public void setResults(String results)
	{
		this.results = results;
	}

	public GregorianCalendar getStartTime()
	{
		return startTime;
	}

	public void setStartTime(GregorianCalendar startTime)
	{
		this.startTime = startTime;
	}

	public GregorianCalendar getEndTime()
	{
		return endTime;
	}

	public void setEndTime(GregorianCalendar endTime)
	{
		this.endTime = endTime;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}
	
	private Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Class.forName("org.sqlite.JDBC");
		String home = System.getProperty("user.home");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");
		
		return conn;
	}
	
	public boolean runHardwareTest()
	{
		//System.out.println("IP Address:" + this.ipAddress);
		
		try{
			Connection conn = getConnection();
			PreparedStatement ps;
						
			this.startTime = new GregorianCalendar();
			Date sTime = new Date();
			
			//System.out.println("Start Time: " + sTime.toString());
			boolean result = Server.invokeHardwareTest(this.ipAddress);
			
			if (result)
			{
				this.results = "PASS";
			}
			else
			{
				this.results = "FAIL";
			}
			
			this.endTime = new GregorianCalendar();
			Date eTime = new Date();
			
			//System.out.println("End Time: " + eTime);
			
//			String q = "insert into hardwaretest (ipAddress, results, start_time, end_time) values ('" + this.ipAddress + "', '" + this.results + "', '" + sTime + "', '" + eTime + "')";
//			stat.executeUpdate(q);
					
			ps = conn.prepareStatement("insert into hardwaretest (ipAddress, results, start_time, end_time) values ( ? , ? , ? , ? )");
			ps.setString(1, this.ipAddress);
			ps.setString(2, this.results);
			ps.setString(3, sTime.toString());
			ps.setString(4, eTime.toString());
			ps.executeUpdate();
			
			conn.close();
			
			//System.out.println("Hardware Test successfully completed");
			
			return result;
						
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in HardwareTestResults - runHardwareTest()");
			return false;
		}		
	}
	
	public List<HardwareTestResult> getAllHardwareTests()
	{
		try{
			Connection conn = getConnection();
			PreparedStatement ps;
			ResultSet rs;
						
			ps = conn.prepareStatement("select * from hardwaretest");
			rs = ps.executeQuery();
			
			List<HardwareTestResult> results = new ArrayList<HardwareTestResult>();
			
			while (rs.next())
			{
				HardwareTestResult r = new HardwareTestResult();
				r.setId(rs.getInt(1));
				r.setIpAddress(rs.getString(3));
				r.setStartTimeString(rs.getString(4));
				r.setEndTimeString(rs.getString(5));
				r.setResults(rs.getString(6));
				results.add(r);
			}
			
			rs.close();
			ps.close();
			conn.close();
			
			//System.out.println("Hardware Test successfully completed");
			return results;
						
		}
		catch (Exception e)
		{
			System.out.println("Exception HIT in HardwareTestResults - runHardwareTest()");
			return null;
		}	
		
	}

}
